package com.green.business.controller;

import com.green.business.domain.Goods;
import com.green.business.service.GoodsService;
import com.green.business.vo.GoodsVo;
import com.green.business.vo.SalesGood;
import com.green.system.common.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Green
 * @Date: 2021/10/22/16:31
 * @Description:
 */

@RequestMapping("goods")
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService ;

    /**
     * 查询
     */
    @RequestMapping("loadAllGoods")
    public Object loadAllGoods(GoodsVo goodsVo){
        return this.goodsService.queryAllGoods(goodsVo);
    }

    /**
     * 查询所有可用商品不分页
     */
    @RequestMapping("getAllAvailableGoods")
    public Object getAllAvailableGoods(){
        return this.goodsService.getAllAvailableGoods();
    }

    /**
     * 根据商品ID查询商品信息
     */
    @RequestMapping("getGoodsByGoodId")
    public Object getGoodsByGoodId(Integer goodsid){
        return new DataGridView(this.goodsService.getById(goodsid));
    }


    /**
     * 根据商品ID查询商品信息
     */
    @RequestMapping("getGoodsByGoodId2")
    public Object getGoodsByGoodId2(Integer goodsid){
        Goods goods = this.goodsService.getById(goodsid);
        if (goods.getAvailable() == Constant.AVAILABLE_FALSE){
            return new SaleFeedBack(-1,"该商品未上架，无法查看");
        }
        return new SaleFeedBack(200,"查看成功",goods);
    }

    /**
     * 根据销售的输入ID查询商品返回数据
     */
    @RequestMapping("getGoodsByGoodIdSales")
    public Object getGoodsByGoodIdSales(Integer id){

        if (null != id){
            Goods goods = this.goodsService.getById(id);
            if (goods == null || goods.equals("")){
                return new SaleFeedBack(-1,"未查出存在此商品");
            }
            if (goods.getAvailable().equals(Constant.AVAILABLE_FALSE)){
                return new SaleFeedBack(-1,"当前商品未上架，扫描失败");
            }
            if (goods.getNumber() <= 0){
                return new SaleFeedBack(-1,"库存已空，该商品扫描失败");
            }
            SalesGood salesGood = new SalesGood();
            salesGood.setId(goods.getId());
            salesGood.setGoodsname(goods.getGoodsname());
            salesGood.setMemberprice(Double.valueOf(String.valueOf(goods.getMemberprice())));
            salesGood.setPrice(goods.getPrice());

            return new SaleFeedBack(200,"扫描成功",salesGood);
        }else {
            return new SaleFeedBack(-1,"当前扫描id值为空");
        }


    }



    /**
     * 添加
     */
    @RequestMapping("addGoods")
    public ResultObj addGoods(Goods goods){
        try{
            goods.setAvailable(Constant.AVAILABLE_TRUE);
            this.goodsService.saveGoods(goods);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }

    }

    /**
     * 更新
     */
    @RequestMapping("updateGoods")
    public ResultObj updateGoods(Goods goods){
        try{
            ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
            goods.setOpretor(activeUser.getUser().getName());
            this.goodsService.updateGoods(goods);  //update() updateSelective()
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }

    }

    /**
     * 下架
     */
    @RequestMapping("downGoods")
    public ResultObj downGoods(Integer id){
        try {
            ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
            this.goodsService.downById(id, activeUser.getUser().getName());
            return ResultObj.DOWN_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DOWN_ERROR;
        }
    }

    /**
     * 批量下架
     */
    @RequestMapping("batchDownGoods")
    public ResultObj batchDownGoods(Integer[] ids){
        try {
            if (null != ids && ids.length > 0){
                List<Integer> idsList = new ArrayList<>();
                for (Integer id : ids) {
                    idsList.add(id);
                }
                ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
                this.goodsService.downByIds(idsList, activeUser.getUser().getName());
                return ResultObj.DOWN_SUCCESS;
            }else{
                return new ResultObj(-1,"传入ID不能为空");
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DOWN_ERROR;
        }
    }

    /**
     * 查询当前商品是否上架
     */
    @RequestMapping("getwhetherAvailableGoods")
    public ResultObj getwhetherAvailableGoods(Integer goodsid){
        try {
            if (goodsid !=null){
                if (this.goodsService.selectWhetherAvailable(goodsid)){
                    return ResultObj.AVAILABLE_NOT_IN;
                }
                return ResultObj.AVAILABLE_IN;
            }
            return new ResultObj(-1,"id为空");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultObj(-1,"查询上架失败");
        }
    }

    /**
     * 库存预警检查，并生成预警公告
     */
    @RequestMapping("warning")
    public ResultObj warning(){
        if (this.goodsService.warning()){
            return new ResultObj(-1,"检测到库存预警请查看系统公告");
        }else {
            return null;
        }
    }
}
